package com.sg.service.biz.pbff.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.pbff.req.BffBatchQueryMidBasicCheckTargetReqDto;
import com.sg.dto.biz.pbff.req.BffQueryFormHorizontalTargetPeriodListReqDto;
import com.sg.dto.biz.pbff.req.BffRefreshExecuteCycleStageTimingRuleReqDto;
import com.sg.dto.biz.pbff.res.BffBatchQueryMidBasicCheckTargetRespDto;
import com.sg.dto.biz.pbff.res.BffQueryFormHorizontalTargetPeriodListRespDto;
import com.sg.dto.biz.pbff.res.BffRefreshExecuteCycleStageTimingRuleRespDto;
import com.sg.service.biz.pbff.TargetCycleService;
import com.wicket.okrapp.integration.TargetCycleService;
import com.wicket.okrapp.integration.dto.*;
import com.wicket.okrpbff.common.dto.TreeNode;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class TargetCycleServiceImpl implements TargetCycleService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.app.TargetCycleService fwAppTargetCycleClient;

    /**
     * B3批量查询中基层校验观测点(管理)[5148]
     * gen by moon at 11/6/2022, 6:40:39 PM
     */
    @Trace(operationName = "B3批量查询中基层校验观测点(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffBatchQueryMidBasicCheckTargetRespDto batchQueryMidBasicCheckTarget(BffBatchQueryMidBasicCheckTargetReqDto reqDto) {


        QueryMidBasicCheckTargetDutyListRespDto queryMidBasicCheckTargetDutyListRespDto_1 = null;
        BatchQueryEvaObjTargetCycleOsetComRespDto batchQueryEvaObjTargetCycleOsetComRespDto_1 = null;
//步骤0: D3查询中基层校验观测点带责任主体列表 - queryMidBasicCheckTargetDutyList
        QueryMidBasicCheckTargetDutyListRespDto queryMidBasicCheckTargetDutyListRespDto = null;
        QueryMidBasicCheckTargetDutyListReqDto queryMidBasicCheckTargetDutyListReqDto = new QueryMidBasicCheckTargetDutyListReqDto();
        if (reqDto != null) {
            queryMidBasicCheckTargetDutyListReqDto.setObjectiveId(reqDto.getObjectiveId());//SimpleFieldAssign//sourceId:423259_1
        }

        /*D3查询中基层校验观测点带责任主体列表[5147]   */
        Assert.isNull(queryMidBasicCheckTargetDutyListReqDto.getObjectiveId(), "B3批量查询中基层校验观测点(管理)-D3查询中基层校验观测点带责任主体列表-指标ID不能为空", false);
        queryMidBasicCheckTargetDutyListRespDto = fwAppTargetCycleClient.queryMidBasicCheckTargetDutyList(queryMidBasicCheckTargetDutyListReqDto).getData();


        queryMidBasicCheckTargetDutyListRespDto_1 = queryMidBasicCheckTargetDutyListRespDto;

//步骤1: D3-3批量查询被评对象目标周期目标设置(公共) - batchQueryEvaObjTargetCycleOsetCom
        BatchQueryEvaObjTargetCycleOsetComRespDto batchQueryEvaObjTargetCycleOsetComRespDto = null;
        if (queryMidBasicCheckTargetDutyListRespDto != null) {
            BatchQueryEvaObjTargetCycleOsetComReqDto batchQueryEvaObjTargetCycleOsetComReqDto = new BatchQueryEvaObjTargetCycleOsetComReqDto();
            batchQueryEvaObjTargetCycleOsetComReqDto.setEvaObjEntityId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:423949_1
            batchQueryEvaObjTargetCycleOsetComReqDto.setEvaObjTableTypeCode("OMS_FRAMEWORK_SPACE");//sourceId:423950_1
            batchQueryEvaObjTargetCycleOsetComReqDto.setEvaObjTypeCode("EVA_OBJECT");//sourceId:423951_1
            batchQueryEvaObjTargetCycleOsetComReqDto.setTreeTypeCode("PLAN_TREE");//sourceId:423952_1
            batchQueryEvaObjTargetCycleOsetComReqDto.setIsArchive("FALSE");//sourceId:423957_1
            batchQueryEvaObjTargetCycleOsetComReqDto.setArchivngType("NOT_ARCHIVE");//sourceId:423958_1
            if (queryMidBasicCheckTargetDutyListRespDto != null) {
                batchQueryEvaObjTargetCycleOsetComReqDto.setEvaObjTargetCycleList(queryMidBasicCheckTargetDutyListRespDto.getMidBasicCheckTargetDutyList().stream().map(item -> item.getObjectiveId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:423262_1
            }

            /*D3-3批量查询被评对象目标周期目标设置(公共)[5145]   */
            Assert.isNull(batchQueryEvaObjTargetCycleOsetComReqDto.getEvaObjEntityId(), "B3批量查询中基层校验观测点(管理)-D3-3批量查询被评对象目标周期目标设置(公共)-被评对象内容表主键ID不能为空", false);
            Assert.isNull(batchQueryEvaObjTargetCycleOsetComReqDto.getEvaObjTableTypeCode(), "B3批量查询中基层校验观测点(管理)-D3-3批量查询被评对象目标周期目标设置(公共)-被评对象内容表类型编码不能为空", false);
            Assert.isNull(batchQueryEvaObjTargetCycleOsetComReqDto.getEvaObjTypeCode(), "B3批量查询中基层校验观测点(管理)-D3-3批量查询被评对象目标周期目标设置(公共)-被评对象类型编码不能为空", false);
            Assert.isNull(batchQueryEvaObjTargetCycleOsetComReqDto.getTreeTypeCode(), "B3批量查询中基层校验观测点(管理)-D3-3批量查询被评对象目标周期目标设置(公共)-树类型编码不能为空", false);
            Assert.isNull(batchQueryEvaObjTargetCycleOsetComReqDto.getIsArchive(), "B3批量查询中基层校验观测点(管理)-D3-3批量查询被评对象目标周期目标设置(公共)-是否存档不能为空", false);
            Assert.isNull(batchQueryEvaObjTargetCycleOsetComReqDto.getArchivngType(), "B3批量查询中基层校验观测点(管理)-D3-3批量查询被评对象目标周期目标设置(公共)-存档类型不能为空", false);
            batchQueryEvaObjTargetCycleOsetComRespDto = fwAppTargetCycleClient.batchQueryEvaObjTargetCycleOsetCom(batchQueryEvaObjTargetCycleOsetComReqDto).getData();


            batchQueryEvaObjTargetCycleOsetComRespDto_1 = batchQueryEvaObjTargetCycleOsetComRespDto;
        }

        BffBatchQueryMidBasicCheckTargetRespDto retData = new BffBatchQueryMidBasicCheckTargetRespDto();
        if (queryMidBasicCheckTargetDutyListRespDto_1 != null) {
            retData.setMidBasicCheckTargetDutyList(queryMidBasicCheckTargetDutyListRespDto_1.getMidBasicCheckTargetDutyList().stream().map(item -> BeanUtil.toBean(item, BffMidBasicCheckTargetDutyDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:423268_1
        }
        if (batchQueryEvaObjTargetCycleOsetComRespDto_1 != null) {
            retData.setCheckEvaObjTargetCycleOsetList(batchQueryEvaObjTargetCycleOsetComRespDto_1.getCheckEvaObjTargetCycleOsetList().stream().map(item -> BeanUtil.toBean(item, BffCheckEvaObjTargetCycleOsetDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:423270_1
        }

// TODO: 2022/11/6 手写AB树代码，后续晓东对接
        List<TreeNode> nodeList = new ArrayList<>();

        for (BffMidBasicCheckTargetDutyDto main : retData.getMidBasicCheckTargetDutyList()) {
            TreeNode node = new TreeNode();
            node.setNLevel(1L);
            node.setSelf(main);
            nodeList.add(node);
            for (BffCheckEvaObjTargetCycleOsetDto sub : retData.getCheckEvaObjTargetCycleOsetList()) {
                if (sub.getTargetCycleContentId().equals(main.getObjectiveId())) {
                    TreeNode subNode = new TreeNode();
                    subNode.setNLevel(2L);
                    subNode.setSelf(sub);
                    node.getChildrenList().add(subNode);
                }
            }
        }
        retData.setTreeNode(nodeList);
        retData.setMidBasicCheckTargetDutyList(null);
        retData.setCheckEvaObjTargetCycleOsetList(null);

        return retData;
    }

    /**
     * B3查询表格横向目标周期列表(管理)[5150]
     * gen by moon at 11/6/2022, 9:25:55 PM
     */
    @Trace(operationName = "B3查询表格横向目标周期列表(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryFormHorizontalTargetPeriodListRespDto queryFormHorizontalTargetPeriodList(BffQueryFormHorizontalTargetPeriodListReqDto reqDto) {


        QueryFormHorizontalTargetPeriodListComRespDto queryFormHorizontalTargetPeriodListComRespDto_1 = null;
//步骤0: D3查询表格横向目标周期列表(公共) - queryFormHorizontalTargetPeriodListCom
        QueryFormHorizontalTargetPeriodListComRespDto queryFormHorizontalTargetPeriodListComRespDto = null;
        QueryFormHorizontalTargetPeriodListComReqDto queryFormHorizontalTargetPeriodListComReqDto = new QueryFormHorizontalTargetPeriodListComReqDto();
        if (reqDto != null) {
            queryFormHorizontalTargetPeriodListComReqDto.setTargetCycleContentId(reqDto.getTargetCycleContentId());//SimpleFieldAssign//sourceId:423314_1
        }

        /*D3查询表格横向目标周期列表(公共)[5149]   */
        Assert.isNull(queryFormHorizontalTargetPeriodListComReqDto.getTargetCycleContentId(), "B3查询表格横向目标周期列表(管理)-D3查询表格横向目标周期列表(公共)-关联目标内容ID不能为空", false);
        queryFormHorizontalTargetPeriodListComRespDto = fwAppTargetCycleClient.queryFormHorizontalTargetPeriodListCom(queryFormHorizontalTargetPeriodListComReqDto).getData();


        queryFormHorizontalTargetPeriodListComRespDto_1 = queryFormHorizontalTargetPeriodListComRespDto;

        BffQueryFormHorizontalTargetPeriodListRespDto retData = new BffQueryFormHorizontalTargetPeriodListRespDto();
        if (queryFormHorizontalTargetPeriodListComRespDto_1 != null) {
            retData.setFormHorizontalTargetPeriodList(queryFormHorizontalTargetPeriodListComRespDto_1.getFormHorizontalTargetPeriodList().stream().map(item -> BeanUtil.toBean(item, BffFormHorizontalTargetPeriodDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:423317_1
        }


        return retData;
    }

    /**
     * B3-更新执行周期阶段时段规则[5540]
     * gen by moon at 12/29/2022, 12:58:50 PM
     */
    @Trace(operationName = "B3-更新执行周期阶段时段规则")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffRefreshExecuteCycleStageTimingRuleRespDto refreshExecuteCycleStageTimingRule(BffRefreshExecuteCycleStageTimingRuleReqDto reqDto) {


        //步骤0: D3-3更新执行周期阶段时段规则(公共) - refreshExecuteCycleStageTimingRuleCom
        RefreshExecuteCycleStageTimingRuleComRespDto refreshExecuteCycleStageTimingRuleComRespDto = null;
        RefreshExecuteCycleStageTimingRuleComReqDto refreshExecuteCycleStageTimingRuleComReqDto = new RefreshExecuteCycleStageTimingRuleComReqDto();
        if (reqDto != null) {
            refreshExecuteCycleStageTimingRuleComReqDto.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:551429_1
            refreshExecuteCycleStageTimingRuleComReqDto.setTargetId(reqDto.getTargetId());//SimpleFieldAssign//sourceId:551430_1
        }

        /*D3-3更新执行周期阶段时段规则(公共)[5541]   */
        Assert.isNull(refreshExecuteCycleStageTimingRuleComReqDto.getEvaluationTemplateId(), "B3-更新执行周期阶段时段规则-D3-3更新执行周期阶段时段规则(公共)-冗余评价模板ID不能为空", false);
        Assert.isNull(refreshExecuteCycleStageTimingRuleComReqDto.getTargetId(), "B3-更新执行周期阶段时段规则-D3-3更新执行周期阶段时段规则(公共)-冗余目标ID不能为空", false);
        refreshExecuteCycleStageTimingRuleComRespDto = fwAppTargetCycleClient.refreshExecuteCycleStageTimingRuleCom(refreshExecuteCycleStageTimingRuleComReqDto).getData();


        BffRefreshExecuteCycleStageTimingRuleRespDto retData = new BffRefreshExecuteCycleStageTimingRuleRespDto();


        return retData;
    }
    //
}
